package com.jlnk.weatherdemo.presenter;

import com.jlnk.weatherdemo.entity.WeatherInfo;
import com.jlnk.weatherdemo.model.WeatherModel;
import com.jlnk.weatherdemo.view.WeatherView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WeatherPresenter {
    private WeatherView view;
    public WeatherPresenter(WeatherView view) {
        this.view = view;
    }
    public void loadWeatherInfo(String city) {
        view.showLoading();
        Call<WeatherInfo> call = WeatherModel.getWeatherInfo(city);
        call.enqueue(new Callback<WeatherInfo>() {
            @Override
            public void onResponse(Call<WeatherInfo> call, Response<WeatherInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    view.hideLoading();
                    view.showWeatherInfo(response.body());
                } else {
                    view.hideLoading();
                    view.showError("Failed to load weather data: " + response.message());
                }
            }
            @Override
            public void onFailure(Call<WeatherInfo> call, Throwable t) {
                view.hideLoading();
                view.showError("Network error: " + t.getMessage());
            }
        });
    }
}